package com.zgf.easy.proxy.demo;

import com.zgf.easy.proxy.EasyProxy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

/**
 * 身份和实现注册管理
 */
@Slf4j
@Configuration
public class IdentityAndImplRegisterCfg {

    private String groovyStr = "package com.zgf.easy.proxy.demo.impla.order\n" +
            "\n" +
            "import com.zgf.easy.proxy.anno.AutoService\n" +
            "import com.zgf.easy.proxy.demo.extend.order.OrderExtendApi\n" +
            "import com.zgf.easy.proxy.demo.extend.order.mode.OrderReq\n" +
            "import com.zgf.easy.proxy.demo.extend.order.mode.OrderRes\n" +
            "import com.zgf.easy.proxy.demo.impla.bean.TestaBeanService\n" +
            "import com.zgf.easy.proxy.handler.BaseHandler\n" +
            "import org.slf4j.Logger\n" +
            "import org.slf4j.LoggerFactory\n" +
            "\n" +
            "@AutoService()\n" +
            "public class GroovyOrderExtendApiImpl extends BaseHandler implements OrderExtendApi {\n" +
            "\n" +
            "    private Logger log = LoggerFactory.getLogger(GroovyOrderExtendApiImpl.class);\n" +
            "\n" +
            "    private TestaBeanService testaBeanService;\n" +
            "\n" +
            "    @Override\n" +
            "    public void init() {\n" +
            "        // 获取springbean\n" +
            "        testaBeanService = application.getBean(TestaBeanService.class);\n" +
            "    }\n" +
            "\n" +
            "    @Override\n" +
            "    public OrderRes getOrder(OrderReq req) {\n" +
            "        OrderRes res = new OrderRes();\n" +
            "        res.setName(req.getName());\n" +
            "        log.info(\"groovy-OrderExtendApiImpl.getOrder={}\", res);\n" +
            "        return res;\n" +
            "    }\n" +
            "\n" +
            "}\n" +
            "\n";

    public IdentityAndImplRegisterCfg(@Autowired EasyProxy easyProxy) {
        try {
            String identity = "groovy";
            //注册groovy类
            easyProxy.registerGroovyImpl(identity, groovyStr);
            //注册groovy身份
            easyProxy.registerModuleInfo("impla", identity);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }



}
